package com.cloudeasy.post.client;

import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.user.UserInfoVO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@FeignClient(value = "cloudeasy-user", fallback = UserClient.UserClientFallback.class)
public interface UserClient {
    /**
     * 验证是否有权限操作
     *
     * @param permissionId  权限Id
     * @param targetAreas   目标操作区域
     * @param operateUserId 操作用户Id
     * @return {@link Result<Boolean>}
     */
    @PostMapping("/can_do/{permissionId}/{operateUserId}")
    Result<Boolean> canDo(@PathVariable("permissionId") String permissionId,
                                 @RequestParam("targetAreas") List<String> targetAreas,
                                 @PathVariable("operateUserId") String operateUserId);

    /**
     * 获取多个用户信息
     *
     * @param ids   用户Id列表
     * @param nicknames 昵称列表
     * @param asc       正序排序字段
     * @param desc      倒序排序字段
     * @return {@link List < UserInfoVO >}的{@link Result}
     */
    @GetMapping("/info/list")
    Result<List<UserInfoVO>> list(@RequestParam(value = "ids", required = false) List<String> ids,
                                  @RequestParam(value = "nicknames", required = false) List<String> nicknames,
                                  @RequestParam(value = "asc", required = false) String[] asc,
                                  @RequestParam(value = "desc", required = false) String[] desc);

    /**
     * 分页获取多个用户信息
     *
     * @param ids   用户Id列表
     * @param nicknames 昵称列表
     * @param asc       正序排序字段
     * @param desc      倒序排序字段
     * @param page      页数
     * @param size      每页大小
     * @return {@link PageResult <UserInfoVO>}的{@link Result}
     */
    @GetMapping("/info/list/{page}/{size}")
    Result<PageResult<UserInfoVO>> page(@RequestParam(value = "ids", required = false) List<String> ids,
                                        @RequestParam(value = "nicknames", required = false) List<String> nicknames,
                                        @RequestParam(value = "asc", required = false) String[] asc,
                                        @RequestParam(value = "desc", required = false) String[] desc,
                                        @PathVariable("page") Integer page,
                                        @PathVariable("size") Integer size);

    @Component
    static class UserClientFallback implements UserClient {
        @Override
        public Result<Boolean> canDo(String permissionId, List<String> targetAreas, String operateUserId) {
            return ResultUtil.failure(ResultCodeEnum.REMOTE_ERROR);
        }

        @Override
        public Result<List<UserInfoVO>> list(List<String> ids, List<String> nicknames, String[] asc, String[] desc) {
            return ResultUtil.failure(ResultCodeEnum.REMOTE_ERROR);
        }

        @Override
        public Result<PageResult<UserInfoVO>> page(List<String> ids, List<String> nicknames, String[] asc, String[] desc, Integer page, Integer size) {
            return ResultUtil.failure(ResultCodeEnum.REMOTE_ERROR);
        }
    }
}
